import pygame
import game_functions as gf
from alien import Alien
from settings import Settings
from ship import Ship
from pygame.sprite import Group
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard


def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    # 创建一个名为screen的显示窗口，
    # 实参(1200,800)是一个元组，指定了游戏窗口的尺寸。
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    # 窗口的标题 Alien Invasion
    pygame.display.set_caption("外星人入侵")

    # 设置背景色
    bg_color = (230, 230, 230)

    # 创建一艘飞船
    ship = Ship(ai_settings, screen)

    # 创建一个用于存储子弹的编组,也就是创建了一个Group实例
    bullets = Group()

    # 创建一个外星人
    # alien = Alien(ai_settings,screen)

    # 创建一个外星人编组
    aliens = Group()

    # 创建外星人群
    gf.create_fleet(ai_settings, screen, ship, aliens)

    # 创建Play按钮
    play_button = Button(ai_settings, screen, "Play")

    # 创建存储游戏统计信息的实例，并创建记分牌
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    # 开始游戏的主循环
    while True:
        gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens,
                        bullets)

        if stats.game_active:
            ship.update()
            gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets)
            gf.update_aliens(ai_settings, stats, screen, sb, ship, aliens, bullets)

        gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets,
                         play_button)


run_game()

# # 开始游戏的主循环
# while True:
#     # 监视键盘和鼠标事件
#     # 当玩家点击play按钮时，游戏便会重置
#     gf.check_events(ai_settings, screen, stats, sb,
#                     play_button, ship, aliens, bullets)
#     if stats.game_active:
#         # 更新飞船的位置
#         ship.update()
#         # 更新子弹位置，并删除已经消失的子弹
#         gf.update_bullets(ai_settings, screen, stats, sb,
#                           ship, aliens, bullets)
#         # 更新外星人位置
#         gf.update_aliens(ai_settings, stats, screen, sb, ship,
#                          aliens, bullets)
#     # 更新屏幕图像
#     gf.update_screen(ai_settings, screen, stats, sb,
#                      ship, aliens, bullets, play_button)



